package com.person.service.impl;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.person.common.BaseContext;
import com.person.mapper.AddressBookMapper;
import com.person.mapper.OrderDetailMapper;
import com.person.mapper.OrderMapper;
import com.person.mapper.ShoppingCartMapper;
import com.person.pojo.*;
import com.person.service.OrderDetailService;
import com.person.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;


    /**
     * 提交订单
     * @param orders
     */
    @Override
    @Transactional
    public void submit(Orders orders) {
        //查询地址，手机号，姓名等信息
        AddressBook addressBook = addressBookMapper.selectAddress(orders.getAddressBookId());

        long id = IdUtil.getSnowflakeNextId();
        orders.setId(id);
        orders.setNumber(id+"");
        orders.setUserId(BaseContext.getCurrentId());
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setUserName(addressBook.getConsignee());
        orders.setConsignee(addressBook.getConsignee());
        orders.setStatus(2);



        //获取购物车
        List<ShoppingCart> shoppingcarts = shoppingCartMapper.getListByShopping(BaseContext.getCurrentId());
        ArrayList<OrderDetail> orderDetails = new ArrayList<>();
        BigDecimal amount = new BigDecimal("0.0");

        //添加订单详情
        for (ShoppingCart shoppingcart : shoppingcarts) {
            BigDecimal price = shoppingcart.getAmount().multiply(new BigDecimal(shoppingcart.getNumber()));
            amount=amount.add(price);
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setId(IdUtil.getSnowflakeNextId());
            orderDetail.setName(shoppingcart.getName());
            orderDetail.setOrderId(orders.getId());
            orderDetail.setDishId(shoppingcart.getDishId());
            orderDetail.setSetmealId(shoppingcart.getSetmealId());
            orderDetail.setDishFlavor(shoppingcart.getDishFlavor());
            orderDetail.setNumber(shoppingcart.getNumber());
            orderDetail.setAmount(amount);
            //添加进集合
            orderDetails.add(orderDetail);
        }


        orders.setAmount(amount);
        log.info("orders订单表：{}",orders);

        //存储订单详情
        log.info("订单详情表：{}",orderDetails);
        orderDetailMapper.save(orderDetails);
        //清空购物车
        shoppingCartMapper.deleteList(BaseContext.getCurrentId());
        //存储订单
        orderMapper.insert(orders);

        }

    @Override
    public PageBean getPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Orders> listPage = orderMapper.getPage(BaseContext.getCurrentId());
        Page<Orders> p =(Page<Orders>) listPage;
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    /**
     * 后台查看订单
     * @param page
     * @param pageSize
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public PageBean getBackendPage(Integer page, Integer pageSize, LocalDateTime beginTime, LocalDateTime endTime,String number) {
        PageHelper.startPage(page,pageSize);
        List<Orders> orderList = orderMapper.getPageAll(beginTime,endTime,number);
        Page<Orders> p = (Page<Orders>) orderList;
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    /**
     * 派送完成
     * @param orders
     * @return
     */
    @Override
    public int orderDelivery(Orders orders) {

        return orderMapper.updateStatus(orders);
    }

}
